<?php
declare (strict_types = 1);
namespace app\user\model\teacher;

use app\common\logic\Attach;
use app\common\model\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 定义讲师数据模型
 */
class Material extends BaseModel
{
    use SoftDelete;
    /**
     * 表名
     * @var string
     */
    protected $name = 'teacher_material';

    /**
     * 设置自动写入时间
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    /**
     * 设置隐藏字段
     * @var array
     */
    protected $hidden = ['update_time', 'delete_time'];

    /**
     * 定义字段类型
     * @var array
     */
    protected $type = [
        'id'            => 'integer',
        'teacher_id'    => 'integer',
        'material_type' => 'integer',
        'category_id'   => 'integer',
        'attach_id'     => 'integer',
        'update_time'   => 'integer',
        'delete_time'   => 'integer',
    ];

    /**
     * 设置类型查询条件
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-07
     */
    public function scopeType($query, $type = 1)
    {
        $query->whereMaterialType($type);
    }

    /**
     * 设置讲师查询条件
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-07
     */
    public function scopeTeacher($query, $id)
    {
        $query->whereTeacherId($id);
    }

    /**
     * 设置默认分类查询条件
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-07
     */
    public function scopeDefault($query)
    {
        $query->whereCategoryId(-1);
    }

    /**
     * 绑定附件基础信息
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-08
     * @return [type] [description]
     */
    public function attach()
    {
        return $this->belongsTo(Attach::class, 'attach_id');
    }

    /**
     * 绑定附件基础信息
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-08
     * @return [type] [description]
     */
    public function attachUrl()
    {
        return $this->belongsTo(Attach::class, 'attach_id')->bind(['attach_url' => 'fileurl']);
    }

    /**
     * 绑定附件基础信息
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-08
     * @return [type] [description]
     */
    public function attachBase()
    {
        return $this->belongsTo(Attach::class, 'attach_id')->bind(['filename', 'filesize', 'duration_format', 'attach_url' => 'fileurl']);
    }
}
